<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends CI_Model {

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	//get all categories
	public function get_all()
	{
		$this->db->select('cate1.id, cate1.name as name, cate1.father_id'
			. ',cate2.name as father_name');
		$this->db->from('categories cate1');
		$this->db->join('categories cate2', 'cate1.father_id = cate2.id', 'left');
		$query = $this->db->get();

		return $query->result();
	}

	//get all categories
	public function get_categories() {
		$query = $this->db->get('categories');
		return $query->result();
	}


	//get category with condition
	public function get_category_by($id, $sub_id) {
		$query = $this->db->query("select * from categories where id != " . $id
			. "and id != " . $sub_id);
		return $query->result();
	}

	//insert
	public function add_new($data, $cond) {
		if($this->db->insert('categories', $data)) {
			$this->db->select('cate1.id, cate1.name as name, cate1.father_id'
			. ',cate2.name as father_name');
			$this->db->from('categories cate1');
			$this->db->join('categories cate2', 'cate1.father_id = cate2.id', 'left');
			$this->db->where($cond);
			$query = $this->db->get();
			return $query->result();
		}else 
			return -1; //insert fail
	}	

	//edit
	public function edit($data, $cond) {
		$this->db->where($cond);
		if($this->db->update('categories', $data))
			return true;
		return false;
	}

	//delete
	public function delete($cond) {
		if($this->db->delete('categories', $cond)) 
			return true;
		return false;
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */